/**
 * Created by yhg97p on 2016/9/27.
 */
define(['app', 'filters/filters'], function(app) {
	app.register.controller('compTask', function($scope, $http, $state, $sce, $stateParams, $rootScope,$cookieStore) {
       // $cookieStore.put('refurbish',1);
        $scope.loading_show = true;
		$scope.load = {
            _getchapterList:function(){
                $scope.allcourse = [];
                $scope.otherMajorList = [];
                $scope.isfirst = [];
                $scope.specialty_id = [];
                if($cookieStore.get('refurbish') == 2){
                    $scope.number = 2;
                }else{
                    $scope.number = 1;
                }
                // $scope.cases = false;
                //老板
                if($scope.app.user.data.role_id == 1) {
                    $scope.myself = false;
                    $scope.otherone = false;
                    $scope.alltask = true;

                    $http({
                        url: app._baseUrl + '/Qy/Task/allSpecialtyList',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    }).success(function(res) {
                        if(res.success){
                            $scope.loading_show = false;
                            $scope.myself =false;
                            $scope.otherone = false;
                            $scope.alltask = true;
                            $scope.Majorinfo = res.data.self_specialty;
                            var arr = [];
                            angular.forEach($scope.Majorinfo,function (item) {
                                angular.forEach(item,function (item1) {
                                    arr.push(item[0].specialty_id)
                                })
                            });
                            //新建数组去重
                            function unique3(array){
                                var n = [array[0]]; //结果数组
                                for(var i = 1; i < array.length; i++) {
                                    if (array.indexOf(array[i]) == i) n.push(array[i]);
                                }
                                return n;
                            }
                            arr1 = unique3(arr);
                            $scope.blank = false;
                            for (var i =0;i<arr1.length;i++){
                                $scope.specialty_Id = arr1[i];
                                $scope.curriculumList(i);
                                $scope.sec.push($scope.specialty_Id)
                            }
                        }else{
                            $scope.blank = true;
                        }
                    });

                    $scope.sec = [];
                    $scope.curriculumList=function(index,specialty_id){
                        if(specialty_id){
                            $scope.sec[index]= specialty_id;
                        }else{
                        }
                        $scope.temp = specialty_id;
                        $http({
                            url: app._baseUrl + '/Qy/Task/curriculumList',
                            method: 'POST',
                            data:{
                                specialty_id:specialty_id||$scope.specialty_Id},
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                        }).success(function(res){
                            $scope.allcourse[index]= res.data;
                        });
                    }
                }
                //非老板
                if($scope.app.user.data.role_id != 1) {
                    $scope.myself = true;
                    $scope.otherone = true;
                    $scope.alltask = false;
                    if($scope.app.user.data.role_id==2){
                        $scope.myself = false;
                    }
                    $http({
                        url: app._baseUrl + '/Qy/Task/allSpecialtyList',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    }).success(function(res) {
                        $scope.loading_show = false;
                        $scope.Majorinfo = res.data;
                        $rootScope.secondary = false;
                        if($scope.Majorinfo.self_specialty!=null){
                            $scope.self = $scope.Majorinfo.self_specialty;
                        }
                        if($scope.self) {
                            $scope.curriculumList($scope.self[0], 0);
                        }
                        $scope.other = $scope.Majorinfo.other_specialty;
                        for(var item in $scope.other) {
                            var object = new Object();
                            object.key = item;
                            object.value = $scope.other[item];
                            $scope.otherMajorList.push(object);
                        }
                        for(var i = 0; i < $scope.otherMajorList.length; i++) {
                            $scope.isfirst[i + 1] = true;
                            $scope.curriculumList($scope.otherMajorList[i].value[0], i + 1, true);
                        }
                    });

                    //课程列表
                    $scope.curriculumList = function(item, index, isfirst) {
                        if(!isfirst) {
                            $scope.isfirst[index] = false;
                        }
                        $scope.specialty_id[index] = item.specialty_id;
                        $http({
                            url: app._baseUrl + '/Qy/Task/curriculumList',
                            method: 'POST',
                            data: {
                                specialty_id: item.specialty_id
                            },
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        }).success(function(res) {
                            $scope.allcourse[index] = res.data;
                        });
                    }
                }
			},
            init: function() {
                this._getchapterList && this._getchapterList();
            }
		}
        $scope.load.init && $scope.load.init();
		if($cookieStore.get('refurbish')==1){
            $scope.number = 1;
        }else{
            $scope.number = 2;
        }
        //随堂任务
        $scope.task = function(){
            $cookieStore.put('refurbish',1);
            $scope.number = 1;
            $scope.load.init && $scope.load.init();
        }
        //随堂进阶
        $scope.case = function(){
        	$cookieStore.put('refurbish',2);
            $scope.number = 2;
            $scope.load.init && $scope.load.init();
        }
        //突破考试
        // $scope.exm = function(){
        //     $scope.number = 3;
        //     $scope.myself = false;
        //     $scope.otherone = false;
        //     $scope.alltask = false;
        //     $scope.exma = true;
        // }



        //前往课程-章节页
		$scope.goTaskCourse = function(item,index) {
			var url = $state.href('app.taskCourse', {
				curriculum_id: item.curriculum_id,
				curriculum_name: item.curriculum_name,
                index:index
			});
			window.location.href = url;
		}

		// $scope.gotrainCourse = function(item) {
		// 	var url = $state.href('app.trainCourse ', {
		// 		curriculum_id: item.curriculum_id
		// 	});
		// 	window.location.href = url;
		// }

	})
})



/**
 * Created by yhg97p on 2016/9/27.
 */
// define(['app', 'filters/filters'], function(app) {
//     app.register.controller('compTask', function($scope, $http, $state, $sce, $stateParams, $rootScope) {
//    		//获取角色信息
// 		$scope.role = $scope.app.user.role_id;
// 		$scope.major = [];
// 		if($scope.role == 1){//角色是老板
// 			$scope.myself = false;
// 			$scope.otherone = false;
// 			$scope.alltask = true;
// 			$http({//请求所有岗位专业列表
// 				url:app._baseUrl + '/Qy/Task/allSpecialtyList',
// 				method:'POST',
//                 headers: {
// 					'Content-Type': 'application/x-www-form-urlencoded'
// 				}
// 			}).success(function(res){
// 				if(res.success){
//                     // courseClick
//                     //给每项专业的每项课程一个点击事件
//                     $scope.courseClick = function(pIndex,index,id){
//                         $scope.thisMajorIndex = pIndex;
//                         $scope.courseIndex = index;
//                         $scope.courseId = id;
//                         $scope.thisCourse = [];
//                         $http({
//                             url:app._baseUrl + '/Qy/Task/curriculumList',
//                             method:'POST',
//                             data:{
//                                 specialty_id:id
//                             },
//                             headers: {
//                                 'Content-Type': 'application/x-www-form-urlencoded'
//                             }
//                         }).success(function(res){
//                             $scope.thisCourse[pIndex] = res.data;
//                         })
//                     }
// 					console.log(res.data.self_specialty)
// 					$scope.major = res.data.self_specialty;//所有专业
// 					$scope.sumIndex = 0;
// 					$scope.arr = [];
// 					angular.forEach($scope.major,function(thisMajor){//遍历出每一项专业
//                         $scope.sumIndex++;
//                         $scope.sumIndexa = $scope.sumIndex - 1;
// 						$scope.thisIndex = 0;
// 						angular.forEach(thisMajor,function(course){
// 							if($scope.thisIndex == 0){
// 								$scope.obj = {
// 									pIndex:$scope.sumIndexa,
// 									index:0,
// 									id:thisMajor[0].specialty_id
// 								}
// 								$scope.arr.push($scope.obj)
//                                 $scope.courseClick($scope.sumIndexa,0,thisMajor[0].specialty_id)
// 							}
// 							$scope.thisIndex ++;
// 						})
// 					})
// 					console.log($scope.arr)
// 				}
// 			})
//
//
//
// 		}else{
//
// 		}
//
//         $scope.goTaskCourse = function(item) {
// 			var url = $state.href('app.taskCourse', {
// 				curriculum_id: item.curriculum_id,
// 				curriculum_name: item.curriculum_name
// 			});
// 			window.location.href = url;
// 		}
//
// 		$scope.gotrainCourse = function(item) {
// 			var url = $state.href('app.trainCourse ', {
// 				curriculum_id: item.curriculum_id
// 			});
// 			window.location.href = url;
// 		}
//
//     })
// })